package com.zx.dsptuneup_TriMic.widget.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;
import com.xujiaji.happybubble.Util;
import com.zx.dsptuneup_TriMic.R;

public class CustomDevMoreDialog extends BubbleDialog implements View.OnClickListener {
    private ViewHolder mViewHolder;
    private OnClickCustomButtonListener mListener;
    private TextView lastSelText = null;

    public CustomDevMoreDialog(Context context) {
        super(context);
        BubbleLayout bl = new BubbleLayout(context);
        bl.setBubbleColor(getContext().getResources().getColor(R.color.color_dev_more_bg));
//        bl.setShadowColor(R.color.home_bg);
        bl.setShadowX(0);
        bl.setShadowY(0);
//        bl.setBubblePadding(0);
//        bl.setLookLength(Util.dpToPx(getContext(), 8));
//        bl.setLookWidth(Util.dpToPx(getContext(), 16));
        bl.setBubbleRadius(Util.dpToPx(getContext(), 3));

        setBubbleLayout(bl);
        View rootView = LayoutInflater.from(context).inflate(R.layout.dev_more_operation, null);
        mViewHolder = new ViewHolder(rootView);
        addContentView(rootView);
        mViewHolder.btn13.setOnClickListener(this);
        mViewHolder.btn14.setOnClickListener(this);
        mViewHolder.btn15.setOnClickListener(this);
        mViewHolder.btn16.setOnClickListener(this);
        mViewHolder.btn17.setOnClickListener(this);
        mViewHolder.btn18.setOnClickListener(this);

//        mViewHolder.btn17.setVisibility(View.GONE);
//        mViewHolder.btn18.setVisibility(View.GONE);

        setPosition(Position.BOTTOM);
    }

    public void setIsParent(boolean isParent){
        if(isParent){
            mViewHolder.btn16.setText("添加子设备");
            mViewHolder.btn13.setVisibility(View.VISIBLE);
            mViewHolder.btn15.setVisibility(View.VISIBLE);
            mViewHolder.btn16.setVisibility(View.VISIBLE);
        }else {
            mViewHolder.btn13.setVisibility(View.GONE);
            mViewHolder.btn15.setVisibility(View.GONE);
            mViewHolder.btn16.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {

        TextView txt = (TextView)v;
        if (mListener != null) {
            mListener.onClick(((TextView)v).getText().toString(),(int)txt.getTag());
        }

        if(lastSelText == null){
            txt.setSelected(true);
            lastSelText = txt;
            return;
        }else {
             if(lastSelText == txt) {
                 return;
             }else {
                 lastSelText.setSelected(false);
                 txt.setSelected(true);
                 lastSelText = txt;
             }
        }

    }

    private static class ViewHolder {
        TextView btn13, btn14, btn15, btn16, btn17, btn18;
        public ViewHolder(View rootView) {
            btn13 = rootView.findViewById(R.id.button13);
            btn14 = rootView.findViewById(R.id.button14);
            btn15 = rootView.findViewById(R.id.button15);
            btn16 = rootView.findViewById(R.id.button16);
            btn17 = rootView.findViewById(R.id.button17);
            btn18 = rootView.findViewById(R.id.button18);

            btn13.setTag(0);
            btn14.setTag(1);
            btn15.setTag(2);
            btn16.setTag(3);
            btn17.setTag(4);
            btn18.setTag(5);
        }
    }

    public void setClickListener(OnClickCustomButtonListener l) {
        this.mListener = l;
    }

    public interface OnClickCustomButtonListener {
        void onClick(String str,int index);
    }
}
